
using System;
using System.Data;
using System.Linq;
using System.Linq.Dynamic;
using System.Linq.Dynamic.Core;
using System.Linq.Expressions;
using System.Threading.Tasks;
using System.Collections.Generic;
using Microsoft.EntityFrameworkCore;

using Abp.UI;
using Abp.AutoMapper;
using Abp.Extensions;
using Abp.Authorization;
using Abp.Domain.Repositories;
using Abp.Application.Services.Dto;
using Abp.Linq.Extensions;


using JYJ.AiYard.StationManagement.TbYardUserParkings;
using JYJ.AiYard.StationManagement.TbYardUserParkings.Dtos;
using JYJ.AiYard.StationManagement.TbYardUserParkings.DomainService;
using JYJ.AiYard.StationManagement.TbYardUserParkings.Authorization;
using JYJ.AiYard.Database;
using JYJ.AiYard.StationManagement.YardReport;
using JYJ.AiYard.StationManagement.YardCommonHald;

namespace JYJ.AiYard.StationManagement.TbYardUserParkings
{
    /// <summary>
    /// TbYardUserParking应用层服务的接口实现方法  
    ///</summary>
    [AbpAuthorize]
    public class TbYardUserParkingAppService : AiYardAppServiceBase, ITbYardUserParkingAppService
    {
        private readonly IRepository<TbYardUserParking, long> _entityRepository;

        private readonly ITbYardUserParkingManager _entityManager;

        private readonly IAiYardDbTableIssuedCard _aiYardDbTableIssuedCard;

        /// <summary>
        /// 构造函数 
        ///</summary>
        public TbYardUserParkingAppService(
        IRepository<TbYardUserParking, long> entityRepository
        ,ITbYardUserParkingManager entityManager
        , IAiYardDbTableIssuedCard aiYardDbTableIssuedCard
        )
        {
            _entityRepository = entityRepository; 
             _entityManager=entityManager;
             _aiYardDbTableIssuedCard = aiYardDbTableIssuedCard;
        }


        /// <summary>
        /// 获取TbYardUserParking的分页列表信息
        ///</summary>
        /// <param name="input"></param>
        /// <returns></returns>
		[AbpAuthorize(TbYardUserParkingPermissions.Query)] 
        public async Task<PagedResultDto<TbYardUserParkingListDto>> GetPaged(GetTbYardUserParkingsInput input)
		{
			List<long> StationList = new List<long>();
			tbQFilterText filterText = input.FilterText.ToObject<tbQFilterText>();
			if (filterText.StationId == null || filterText.StationId <= 0)
			{
				StationList = GetCurrenUserOrganizationAndChildrenUnit();
			}
			else
			{
				StationList.Add((long)filterText.StationId);
			}
			var query1 = _aiYardDbTableIssuedCard.GetTbYardUserParkinr(StationList, filterText.FilterText);

            var query = query1.AsQueryable();
            var count = query.Count();

            //var count = await query.CountAsync();

            var entityList = query
                .OrderBy(input.Sorting).AsNoTracking()
                .PageBy(input)
                .ToList(); 
            var entityListDtos =entityList.MapTo<List<TbYardUserParkingListDto>>();

			return new PagedResultDto<TbYardUserParkingListDto>(count,entityListDtos);
		}


		/// <summary>
		/// 通过指定id获取TbYardUserParkingListDto信息
		/// </summary>
		[AbpAuthorize(TbYardUserParkingPermissions.Query)] 
		public async Task<TbYardUserParkingListDto> GetById(EntityDto<long> input)
		{
			var entity = await _entityRepository.GetAsync(input.Id);

		    return entity.MapTo<TbYardUserParkingListDto>();
		}

		/// <summary>
		/// 获取编辑 TbYardUserParking
		/// </summary>
		/// <param name="input"></param>
		/// <returns></returns>
		[AbpAuthorize(TbYardUserParkingPermissions.Create,TbYardUserParkingPermissions.Edit)]
		public async Task<GetTbYardUserParkingForEditOutput> GetForEdit(NullableIdDto<long> input)
		{
			var output = new GetTbYardUserParkingForEditOutput();
            TbYardUserParkingEditDto editDto;

			if (input.Id.HasValue)
			{
				var entity = await _entityRepository.GetAsync(input.Id.Value);

				editDto = entity.MapTo<TbYardUserParkingEditDto>(); 
			}
			else
			{
				editDto = new TbYardUserParkingEditDto();
			}

			output.TbYardUserParking = editDto;
			return output;
		}


		/// <summary>
		/// 添加或者修改TbYardUserParking的公共方法
		/// </summary>
		/// <param name="input"></param>
		/// <returns></returns>
		[AbpAuthorize(TbYardUserParkingPermissions.Create,TbYardUserParkingPermissions.Edit)]
		public async Task CreateOrUpdate(CreateOrUpdateTbYardUserParkingInput input)
		{
            if (input.TbYardUserParking.Station_ID <= 0)
            {
                throw new UserFriendlyException("请选择确定的场所名称");
            }
            if (input.TbYardUserParking.Id.HasValue)
			{
				await Update(input.TbYardUserParking);
			}
			else
			{
				await Create(input.TbYardUserParking);
			}
		}


		/// <summary>
		/// 新增TbYardUserParking
		/// </summary>
		[AbpAuthorize(TbYardUserParkingPermissions.Create)]
		protected virtual async Task Create(TbYardUserParkingEditMoreDto input)
		{
             
            //添加多条记录当为4则添加1、2、3、4  则规定最大为10
            if (input.ParkingCount > 0)
            {
                if (input.ParkingCount > 100)
                    throw new UserFriendlyException("车位数量不能大于100!");
                else
                {
                    for (int i = 1; i <= input.ParkingCount; i++)
                    {
                        input.ParkingPos = i.ToString();
                        var entity = input.MapTo<TbYardUserParking>();
                        await _entityRepository.InsertAsync(entity);
                    }         
                }
            }
            else
            {               
                var entity = input.MapTo<TbYardUserParking>();
                await _entityRepository.InsertAsync(entity);
            } 
            
		}

		/// <summary>
		/// 编辑TbYardUserParking
		/// </summary>
		[AbpAuthorize(TbYardUserParkingPermissions.Edit)]
		protected virtual async Task Update(TbYardUserParkingEditMoreDto input)
		{
			//TODO:更新前的逻辑判断，是否允许更新

			var entity = await _entityRepository.GetAsync(input.Id.Value);
            entity.ParkingPos = input.ParkingPos;
             
            await _entityRepository.UpdateAsync(entity);
		}



		/// <summary>
		/// 删除TbYardUserParking信息的方法
		/// </summary>
		/// <param name="input"></param>
		/// <returns></returns>
		[AbpAuthorize(TbYardUserParkingPermissions.Delete)]
		public async Task Delete(EntityDto<long> input)
		{
			//TODO:删除前的逻辑判断，是否允许删除
			await _entityRepository.DeleteAsync(s => s.Id==input.Id);
		}



		/// <summary>
		/// 批量删除TbYardUserParking的方法
		/// </summary>
		[AbpAuthorize(TbYardUserParkingPermissions.BatchDelete)]
		public async Task BatchDelete(List<long> input)
		{
			// TODO:批量删除前的逻辑判断，是否允许删除
			await _entityRepository.DeleteAsync(s => input.Contains(s.Id));
		}


		///// <summary>
		///// 导出TbYardUserParking为excel表,等待开发。
		///// </summary>
		///// <returns></returns>
		//public async Task<FileDto> GetToExcel()
		//{
		//	var users = await UserManager.Users.ToListAsync();
		//	var userListDtos = ObjectMapper.Map<List<UserListDto>>(users);
		//	await FillRoleNames(userListDtos);
		//	return _userListExcelExporter.ExportToFile(userListDtos);
		//}

    }
}


